<template>
    <div class="page-container">
        <div class="grid-form-container">
            <div class="operation">
                <el-button @click="lockUser">禁用</el-button>
                <el-button @click="unlockUser">启用</el-button>
            </div>
            <div class="search">
                <el-input v-model="query.username" class="search-input-class" placeholder="用户名" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="grid-container">
            <el-table ref="userTable" :data="table.data" border @row-click="handleClickTableRow" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="username" label="用户名" />
                <el-table-column prop="nickname" label="昵称" />
                <el-table-column prop="state" label="状态" :formatter="stateFormatter" />
                <el-table-column label="操作" width="160">
                    <template #default="scope">
                        <el-popconfirm title="确认重置？" confirmButtonText='确定' cancelButtonText='再考虑一下' @confirm="pwdInit(scope.row)" v-if="scope.row.state === 0">
                            <template #reference>
                                <el-button size="small">重置密码</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="table.pagination.currentPage" :page-sizes="table.pagination.pageSizes" :page-size="table.pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="table.pagination.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { ref, toRefs, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import http from '@/http.js'

export default {
    name: 'User',
    setup() {
        const router = useRouter();
        const route = useRoute();
        const userTable = ref(null);

        const state = reactive({
            query: {
                username: '',
            },
            table: {
                data: [],
                selected: [],
                pagination: {
                    currentPage: 1,
                    pageSizes: [10, 20, 50, 100],
                    pageSize: 10,
                    total: 0
                }
            },
        });

        const handleSelectionChange = (val) => {
            state.table.selected = val;
        }

        const handleSizeChange = (val) => {
            state.table.pagination.pageSize = val;
            loadData();
        };

        const handleCurrentChange = (val) => {
            state.table.pagination.currentPage = val;
            loadData();
        };

        const handleClickTableRow = (row) => {
            userTable.value.clearSelection();
            userTable.value.toggleRowSelection(row);
        }

        const loadData = () => {
            let params = {
                username: state.query.username,
                page: state.table.pagination.currentPage,
                pageSize: state.table.pagination.pageSize
            }
            http.fetchForm({
                url: '/sys/user/queryGrid',
                data: params
            }).then((res) => {
                state.table.data = res.data.grid;
                state.table.pagination.total = res.data.total;
            });
        }

        const stateFormatter = (row, column, cellValue, index) => {
            let data = row[column.property];
            if (data == 0) {
                return "正常";
            } else {
                return "已锁定";
            }
        }

        const reset = () => {
            state.query.username = '';
            loadData();
        }

        const lockUser = () => {
            let selected = state.table.selected;
            if (selected.length == 0) {
                ElNotification({
                    title: '提示',
                    message: "请选择要禁用的用户",
                    type: 'info'
                });
                return;
            }
            http.fetchForm({
                url: '/sys/user/lockUser',
                method: 'post',
                data: {
                    id: selected[0].id
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "操作成功",
                    type: 'success'
                });
                loadData();
            });
        }

        const unlockUser = () => {
            let selected = state.table.selected;
            if (selected.length == 0) {
                ElNotification({
                    title: '提示',
                    message: "请选择要启用的用户",
                    type: 'info'
                });
                return;
            }
            http.fetchForm({
                url: '/sys/user/unlockUser',
                method: 'post',
                data: {
                    id: selected[0].id
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "操作成功",
                    type: 'success'
                });
                loadData();
            });
        }

        const pwdInit = (row) => {
            http.fetchJson({
                url: '/sys/user/passwordInit',
                data: {
                    id: row.id
                }
            }).then((res) => {
                ElNotification({
                    title: '提示',
                    message: "重置成功",
                    type: 'success'
                });
            });
        }

        // mounted
        onMounted(() => {
            loadData();
        });

        return {
            ...toRefs(state),
            userTable,
            handleSelectionChange,
            handleSizeChange,
            handleCurrentChange,
            handleClickTableRow,
            loadData,
            stateFormatter,
            reset,
            lockUser,
            unlockUser,
            pwdInit
        };
    }
}
</script>
<style scoped>
.page-container {
    width: 100%;
    height: 100%;
}

.grid-form-container {
    width: 100%;
    padding: 20px 10px 10px 10px;
    display: flex;
    justify-content: space-between;
}

.search {
    display: flex;
}

.search-input-class {
    width: 180px;
    margin-right: 10px;
}

.grid-container {
    padding: 10px 10px 0 10px;
}

.pagination-container {
    padding: 10px;
}

.dialog-content {
    padding: 10px 20px;
}

.label-class {
    width: 100%;
    padding: 10px 0;
    font-size: 12px;
    color: #657180;
    font-weight: bold;
}

.content-class {
    width: 100%;
    padding: 0 0 10px 0;
}

.form-input-class {
    width: 100%;
}
</style>